package com.example.rabbitboot;

import com.rabbitmq.client.AMQP;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * @author cxx
 * @create 2020-02-21 0:18
 **/
@Configuration
public class RabbitConfiguration {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange ("direct");
    }

    @Bean
    public Queue queue(){
        return new Queue ("my_boot_1");
    }

    @Bean
    public Binding binding(){
       return BindingBuilder.bind (queue()).to (directExchange ()).with ("mykey");
    }

    @PostConstruct
    public void init(){
        rabbitTemplate.setMandatory (true);
        rabbitTemplate.setConfirmCallback ((data,ack,cause)->{
            System.out.println ("ack:"+ack);
            if (!ack){
//                data.getReturnedMessage ().getBody ()
            }
        });
    }

}
